package com.sixiaolong.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixiaolong.common.R;
import com.sixiaolong.domain.Employee;
import com.sixiaolong.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

//    员工登录
    @PostMapping("/login")
    public R<Employee> getLogin(HttpServletRequest request, @RequestBody Employee employee){

//        employeeService.getById(request.get)
        String password = employee.getPassword();
//        1:md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        2:根据用户名查找数据库 有没有该用户
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername,employee.getUsername());
//        System.out.println(password);
        Employee emp = employeeService.getOne(lqw);
        if (emp == null) return R.error("用户名不存在，请输入正确的用户名");
//       3:比对密码是否正确
        if (!emp.getPassword().equals(password)) return R.error("密码不正确，请输入正确的密码");
//       4:查看员工状态 是否为禁用
        if(emp.getStatus() == 0) return R.error("该用户已被禁用");
//        5：登录成功，将员工id存入session中并返回
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

//    员工退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

//    新增员工
    @PostMapping()
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
//        设置初始密码 并用md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
//        设置创建者  为从session中取得的
        //employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        //employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
//        判断账号是否存在 不存在
//        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Employee::getUsername,employee.getUsername());
//        Employee one = employeeService.getOne(lqw);
//        if (one != null){
//            return R.error("该用户已存在，请重新添加");
//        }
        boolean save = employeeService.save(employee);
        if (save){
            return R.success("新增成功");
        }
        return R.error("新增失败");
    }

//    分页查询
    @GetMapping("/page")
    public R<Page> getpage(int page,int pageSize,String name){
//        构造分页器
        Page pageinfo = new Page(page, pageSize);
//        设置过滤条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(name), Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);
//    查询分页
        employeeService.page(pageinfo,lqw);
        return R.success(pageinfo);
    }

//    启用禁用账号 以及 修改保存
    @PutMapping()
    public R<String> updateStatus(HttpServletRequest request,@RequestBody Employee employee){
        long id = Thread.currentThread().getId();
        log.info("修改线程id为："+id);
//        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Employee::getId,employee.getId());
//        Employee one = employeeService.getOne(lqw);
//        Integer status = one.getStatus();
//        if (employee.getStatus() == 0) {
//            employee.setStatus(1);
//        } else {
//            employee.setStatus(0);
//        }
//        if (employee.getStatus() == 1){
//            employee.setStatus(0);
//        }else {
//            employee.setStatus(1);
//        }
//        修改人
        //employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
//        修改时间
//        employee.setUpdateTime(new Date());
        //employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 获取对应id的员工信息
     */
    @GetMapping("/{id}")
    public R<Employee> selectEmployee(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }else {
            return R.error("没有查询到对应的员工信息");
        }

    }
    /**
     * 修改员工信息
     * @param employee
     * @return9
     */
//    @PutMapping()
//    public R<String> updateEmployee(HttpServletRequest request, @RequestBody Employee employee){
//        //employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
//        //employee.setUpdateTime(LocalDateTime.now());
//        employeeService.updateById(employee);
//        return R.success("保存成功");
//    }
}
